package com.bml.architect.framework.protocol.http;

import com.bml.architect.framework.Invocation;
import org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpClient {


    public String send(String hostname, int port, Invocation invocation) {

        //jdk11的用法

//        var request = HttpRequest.newBuilder()
//                .uri(new URI("http", null, hostname, port, "/", null, null))
//                .POST(HttpRequest.BodyPublishers.ofString(JSONObject.toJSONString(invocation)))
//                .build();
//        var client = java.net.http.HttpClient.newHttpClient();
//
//        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
//
//        String result = response.body();
        //jdk8 之前的用法
        try {
            URL url = new URL("http", hostname, port, "/");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);

            OutputStream outputStream = httpURLConnection.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outputStream);

            oos.writeObject(invocation);
            oos.flush();
            oos.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            String result = IOUtils.toString(inputStream);
            return result;
        } catch (Exception ce) {
            ce.printStackTrace();
        }
        return null;
    }
}
